package cn.itcast.demo;

import cn.itcast.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;

@WebServlet("/listStudentServlet")
public class listStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        读取文档
//        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\student.txt"));
//        ServletOutputStream os = resp.getOutputStream();
//        byte[] bytes = new byte[1024];
//        int len;
//        while ((len = bis.read(bytes))!= -1){
//            os.write(bytes,0,len);
//        }
//        bis.close();
        BufferedReader br = new BufferedReader(new FileReader("d:\\student.txt"));
        ArrayList<Student> list = new ArrayList<>();
        String s;
        while ((s = br.readLine())!= null){
            String[] split = s.split(",");
            Student stu = new Student(split[0],Integer.parseInt(split[1]),Integer.parseInt(split[2]));
            list.add(stu);

        }
        resp.setContentType("text/html;charset=UTF-8");
        for (Student student : list) {
            resp.getWriter().println(student);

        }
//        跳转页面
//        resp.setContentType("text/html;charset=UTF-8");
//        resp.getWriter().write("");
}

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
